package com.meitianhui.convenitenbuy;

import android.app.Application;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.os.Build;
import android.os.StrictMode;
import android.support.multidex.MultiDex;
import android.util.Log;
import android.widget.Toast;

import com.alibaba.baichuan.android.trade.AlibcTradeSDK;
import com.alibaba.baichuan.android.trade.callback.AlibcTradeInitCallback;
import com.baidu.mapapi.SDKInitializer;
import com.meitianhui.convenitenbuy.di.AppComponent;
import com.meitianhui.convenitenbuy.di.AppModule;
import com.meitianhui.convenitenbuy.di.DaggerAppComponent;
import com.meitianhui.convenitenbuy.hyphenate.HyphenateHelper;
import com.meitianhui.convenitenbuy.module.home.model.database.DaoMaster;
import com.meitianhui.convenitenbuy.module.home.model.database.DaoSession;
import com.meitianhui.convenitenbuy.util.BtnMenuDialogResourceUtil;
import com.meitianhui.convenitenbuy.util.TipUtils;
import com.umeng.socialize.PlatformConfig;
import com.umeng.socialize.UMShareAPI;

import cn.jpush.android.api.JPushInterface;


public class App extends Application {
    private static final String TAG = "App";
    public static App mApp;
    private AppComponent mAppComponent;
    private SQLiteDatabase db;
    private DaoSession mDaoSession;

    public boolean debugMode = false;//默认true为测试模式，为false为正式开发模式

    public synchronized static App getInstance() {
        if (mApp == null) {
            mApp = new App();
        }
        return mApp;
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        Log.d(TAG, "attachBaseContext");
        MultiDex.install(this);
    }

    @Override
    public void onCreate() {
        super.onCreate();

        if (debugMode){
            //如果是测试环境
            Constants.HOST = "https://t-appportal.meitianhui.com/openapi/";
            Constants.FILE_HOST = "https://t-appportal.meitianhui.com/";
            Constants.APP_ID = "bcc1cf2e-b376-11e5-921c-fcaa1490ccaf";
            Constants.PRIVATE_KEY = "690b98fcb37711e5921cfcaa1490ccaf717130d1b37711e5921cfcaa1490ccaf";   //测试的appid

        }else {
            //正式环境
            Constants.HOST = "https://appportal.meitianhui.com/openapi/";
            Constants.FILE_HOST = "https://appportal.meitianhui.com/";
            Constants.APP_ID = "c1ec052c-e5df-44b2-baa6-e88527aa2e03";
            Constants.PRIVATE_KEY = "d679a929959e4f0093051b7d7d732ed61e4e8ee3b23047429e72b845b506bfa1";   //正式的appid

        }
        Log.d(TAG, "onCreate start");
        mApp = this;
        HyphenateHelper.getInstance().init(getApplicationContext());
        SDKInitializer.initialize(getApplicationContext());
        //第三方分享初始化
        UMShareAPI.get(this);
        PlatformConfig.setWeixin(Constants.WX_APP_ID, Constants.WX_AppSecret);
        PlatformConfig.setQQZone(Constants.QQ_ID, Constants.QQ_KEY);
        mAppComponent = DaggerAppComponent.builder()
                .appModule(new AppModule(this))
                .build();
        BtnMenuDialogResourceUtil.setContext(getApplicationContext());
        setDatabase();

        JPushInterface.setDebugMode(false);    // 设置开启日志,发布时请关闭日志
        JPushInterface.init(this);


        //电商SDK初始化
        AlibcTradeSDK.asyncInit(this, new AlibcTradeInitCallback() {
            @Override
            public void onSuccess() {
//                Map utMap = new HashMap<>();
//                utMap.put("debug_api_url","http://muvp.alibaba-inc.com/online/UploadRecords.do");
//                utMap.put("debug_key","baichuan_sdk_utDetection");
//                UTTeamWork.getInstance().turnOnRealTimeDebug(utMap);
//                AlibcUserTracker.getInstance().sendInitHit4DAU("19","3.1.1.100");

            }

            @Override
            public void onFailure(int code, String msg) {
                TipUtils.showToast(App.getInstance(), "初始化失败,错误码="+code+" / 错误消息="+msg, Toast.LENGTH_SHORT);
            }
        });

        //解决android N（>=24）系统以上分享 路径为file://时的 android.os.FileUriExposedException异常
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
            StrictMode.setVmPolicy(builder.build());
        }
    }

    private void setDatabase() {
        DaoMaster.DevOpenHelper  helper = new DaoMaster.DevOpenHelper(this, "MeiTianHui-db");
        db = helper.getWritableDatabase();
        DaoMaster daoMaster = new DaoMaster(db);
        mDaoSession = daoMaster.newSession();
    }

    public DaoSession getDaoSession() {
        return mDaoSession;
    }

    public SQLiteDatabase getDb() {
        return db;
    }

    public AppComponent getAppComponent() {
        return mAppComponent;
    }
}
